/*
 * For modifications by Bob Breznak
 * 
 * Copyright (c) 2006, Bob Breznak
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 *    * Redistributions of source code must retain the above copyright notice,
 *      this list of conditions and the following disclaimer.
 *    * Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *    
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 * POSSIBILITY OF SUCH DAMAGE.
 */
/*
 * Copyright 2006 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.iisageek.client;

import java.util.ArrayList;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.SourcesClickEvents;
import com.google.gwt.user.client.ui.StackPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import org.gwtwidgets.client.ui.PNGImage;

/**
 * Shortcuts
 * 
 * the panel on the left side of the screen. Acts as the main navigation
 * 
 * Singleton.
 * 
 * @author Bob Breznak
 */
public class Shortcuts extends Composite {

	// Allows for only one instance of Shortcuts
	private static Shortcuts singleton;
	
	private StackPanel stackPanel = new StackPanel();

	private ArrayList members = new ArrayList();
	private ArrayList equipment = new ArrayList();
	private ArrayList workorders = new ArrayList();
	private ArrayList meetings = new ArrayList();
	private ArrayList training = new ArrayList();
	
	public static final String MEMBERS_TAG = "members_";
	public static final String EQUIPMENT_TAG = "equipment_";
	public static final String WORKORDERS_TAG = "workorders_";
	public static final String MEETINGS_TAG = "meetings_";
	public static final String TRAINING_TAG = "training_";
	
	/**
	 * Shortcuts
	 * 
	 * Creates a shortcut panel on the left side
	 */
	public Shortcuts() {
		
		singleton = this;
		
		members.add(new ShortcutLink("New Member", "images/icons/newmember.png", MEMBERS_TAG + "newmember"));
		members.add(new ShortcutLink("Officers", "images/icons/officers.png", MEMBERS_TAG + "officers"));
		members.add(new ShortcutLink("Active", "images/icons/active.png", MEMBERS_TAG + "activemembers"));
		members.add(new ShortcutLink("Associate", "images/icons/associate.png", MEMBERS_TAG + "associatemembers"));
		members.add(new ShortcutLink("Away", "images/icons/away.png", MEMBERS_TAG + "awaymembers"));
		members.add(new ShortcutLink("Alum", "images/icons/alum.png", MEMBERS_TAG + "alummembers"));
		members.add(new ShortcutLink("Directory", "images/icons/directory.png", MEMBERS_TAG + "directory"));
		
		equipment.add(new ShortcutLink("Add Inventory", "images/icons/newitem.png", EQUIPMENT_TAG + "addinventory"));
		equipment.add(new ShortcutLink("Add Documents","images/icons/newdocument.png", EQUIPMENT_TAG + "adddequipmentocuments"));
		equipment.add(new ShortcutLink("List Inventory", "images/icons/listitems.png", EQUIPMENT_TAG + "listinventory"));
		equipment.add(new ShortcutLink("List Documents", "images/icons/listdocuments.png", EQUIPMENT_TAG + "listequipmentdocuments"));
		equipment.add(new ShortcutLink("Categories", "images/icons/edit.png", EQUIPMENT_TAG + "editequipmentcategories"));
		
		workorders.add(new ShortcutLink("Calendar", "images/icons/calendar.png",WORKORDERS_TAG + "eventscalendar"));
		workorders.add(new ShortcutLink("Pending", "images/icons/pending.png",WORKORDERS_TAG + "editequipmentcategories"));
		workorders.add(new ShortcutLink("Current", "images/icons/workorders.png",WORKORDERS_TAG + "editequipmentcategories"));
		workorders.add(new ShortcutLink("Invoices", "images/icons/billing.png",WORKORDERS_TAG + "eventinvoices"));
		workorders.add(new ShortcutLink("Hours", "images/icons/hours.png",WORKORDERS_TAG + "eventhours"));
		workorders.add(new ShortcutLink("Reports", "images/icons/reports.png", WORKORDERS_TAG + "eventreports"));
		workorders.add(new ShortcutLink("All", "images/icons/allevents.png", WORKORDERS_TAG + "allworkorders"));
		workorders.add(new ShortcutLink("Archive", "images/icons/archives.png", WORKORDERS_TAG + "workorderarchives"));
		
		meetings.add(new ShortcutLink("New Notice", "images/icons/newnotice.png", MEETINGS_TAG + "newmeetingnotice"));
		meetings.add(new ShortcutLink("Attendance", "images/icons/attendance.png", MEETINGS_TAG + "newmeetingnotice"));
		meetings.add(new ShortcutLink("Crew List", "images/icons/crewlist.png", MEETINGS_TAG + "newmeetingnotice"));
		meetings.add(new ShortcutLink("Past Notices", "images/icons/pastnotices.png", MEETINGS_TAG + "newmeetingnotice"));

		training.add(new ShortcutLink("New Training", "images/icons/list.png", TRAINING_TAG + "newtraining"));
		training.add(new ShortcutLink("Warnings", "images/icons/warning.png", TRAINING_TAG + "warnings"));
		training.add(new ShortcutLink("Edit Training", "images/icons/edit.png", TRAINING_TAG + "edittraining"));
		
		// Create the groups within the stack panel.
		stackPanel.add(new ShortcutController(members), createHeaderHTML(new PNGImage(
				"images/icons/members.png", 16, 16), "Members"), true);
		stackPanel.add(new ShortcutController(workorders), createHeaderHTML(new PNGImage(
				"images/icons/events.png", 16, 16), "Events"), true);
		stackPanel.add(new ShortcutController(meetings), createHeaderHTML(new PNGImage(
				"images/icons/meetings.png", 16, 16), "Meetings"), true);
		stackPanel.add(new ShortcutController(equipment), createHeaderHTML(
				new PNGImage("images/icons/equipment.png", 16, 16), "Equipment"),
				true);
		stackPanel.add(new ShortcutController(training), createHeaderHTML(new PNGImage(
				"images/icons/training.png", 16, 16), "Training"), true);

		stackPanel.showStack(0);

		initWidget(stackPanel);
	}
	
	/**
	 * setMode
	 * 
	 * Changes the content of the main panel
	 * 
	 * @param mode (String) the link's tolkin
	 */
	private void setMode(String mode) {
		if(mode.substring(0, MEMBERS_TAG.length()).equalsIgnoreCase(MEMBERS_TAG)){
			MainPanel.get().setMode(MainPanel.MEMBERS, mode);
		} else if(mode.substring(0, WORKORDERS_TAG.length()).equalsIgnoreCase(WORKORDERS_TAG)){
			MainPanel.get().setMode(MainPanel.WORKORDERS, mode);
		} else {
			lnl.get().showError();
		}
	}
	
	/**
	 * get
	 * 
	 * Returns the single instance of Shortcuts
	 * 
	 * @return Shortcuts
	 */
	public static Shortcuts get() {
		return singleton;
	}

	/**
	 * Creates an HTML fragment that places an image & caption together, for use
	 * in a group header.
	 * 
	 * @param imageUrl
	 *            the url of the icon image to be used
	 * @param caption
	 *            the group caption
	 * @return the header HTML fragment
	 */
	private String createHeaderHTML(PNGImage image, String caption) {
		return "<table align='left'><tr>"
				+ "<td><img src='"
				+ image.getUrl()
				+ "'></td>"
				+ "<td style='vertical-align:middle'><b style='white-space:nowrap'>"
				+ caption + "</b></td>" + "</tr></table>";
	}


	private class ShortcutController extends Composite implements	ClickListener {
		private ArrayList links = new ArrayList();

		private VerticalPanel panel = new VerticalPanel();

		public ShortcutController(ArrayList links) {
			for(int i=0;i<links.size();i++) {
				add((ShortcutLink)links.get(i));
			}
			
			initWidget(panel);
			setStyleName("shortcut-Nav");
		}

		public void add(ShortcutLink link) {
			links.add(link);
			panel.add(link);
			link.addClickListener(this);
		}

		public void onClick(Widget sender) {
			setMode(((Hyperlink) sender).getTargetHistoryToken());
		}
	}

	private class ShortcutLink extends Composite implements SourcesClickEvents {

		private Hyperlink link;
		private String tolkin;

		/**
		 * ShortcutLink
		 * 
		 * Creates a link that contains an image, history tokin, and link to a
		 * secion. To be used on a shortcut.
		 * 
		 * @param title
		 *            The title of the link
		 * @param image
		 *            The path to the icon
		 */

		public ShortcutLink(String title, String image, String tolkin) {
			HorizontalPanel panel = new HorizontalPanel();

			
			link = new Hyperlink(title, tolkin);

			panel.add(new PNGImage(image, 16, 16));
			panel.add(link);
			panel.add(new HTML("<br />"));

			initWidget(panel);
		}

		/**
		 * addClickListener
		 * 
		 * @param listener
		 *            A clicklistener that will be executed when the link is
		 *            clicked
		 */
		public void addClickListener(ClickListener listener) {
			link.addClickListener(listener);
		}

		/**
		 * removeClickListener
		 * 
		 * @param listener
		 *            A clicklistener that will be removed from the link
		 */
		public void removeClickListener(ClickListener listener) {
			link.removeClickListener(listener);
		}

		/**
		 * equals
		 * 
		 * Returns true if the given link is equal to this link
		 * 
		 * @param obj
		 *            An object to be compaired against this link
		 */
		public boolean equals(Object obj) {
			return link.getTargetHistoryToken().equals(obj);
		}

		/**
		 * getTolkin
		 * 
		 * Returns the link tolkin.
		 * 
		 * @return
		 */
		public String getTolkin() {
			return tolkin;
		}
	}

}
